#!/bin/bash

# 串口传感器数据识别工具
# 使用方法: ./scripts/identify_sensor.sh [串口路径]

echo "=== 串口传感器数据识别工具 ==="
echo ""

# 默认串口
PORT="${1:-/dev/ttyCH9344USB0}"

if [ ! -e "$PORT" ]; then
    echo "❌ 错误: 串口 $PORT 不存在"
    echo ""
    echo "可用串口:"
    ls -l /dev/tty* | grep -E "CH9344|USB" || echo "  未找到 USB 串口"
    exit 1
fi

echo "📡 监听串口: $PORT"
echo "   波特率: 9600"
echo "   正在读取数据 (Ctrl+C 退出)..."
echo ""

# 读取串口数据并识别
stty -F $PORT 9600 cs8 -cstopb -parenb raw
exec 3<$PORT

count=0
while IFS= read -r -u 3 -n 18 line; do
    count=$((count + 1))

    # 显示原始数据
    echo "[$count] 原始数据: $line"

    # 识别传感器类型
    if echo "$line" | grep -qE "R:[0-9]{3}\.[0-9]RH [0-9]{3}\.[0-9]C"; then
        echo "    ✅ 类型: 温湿度传感器 (temperature)"
        echo "    📊 DeviceID 建议: TH_SENSOR_$(date +%Y%m%d_%H%M%S)"

        # 提取温湿度数值
        humidity=$(echo "$line" | sed -n 's/.*:\([0-9]*\.[0-9]\)RH.*/\1/p')
        temperature=$(echo "$line" | sed -n 's/.*RH \([0-9]*\.[0-9]\)C.*/\1/p')
        echo "    🌡️  温度: ${temperature}°C, 湿度: ${humidity}%"

    elif echo "$line" | grep -qE "CO2:[0-9]+ppm"; then
        echo "    ✅ 类型: CO2 传感器 (co2)"
        echo "    📊 DeviceID 建议: CO2_SENSOR_$(date +%Y%m%d_%H%M%S)"

        value=$(echo "$line" | sed -n 's/.*CO2:\([0-9]*\)ppm.*/\1/p')
        echo "    💨 CO2: ${value} ppm"

    elif echo "$line" | grep -qE "CO:[0-9]+ppm"; then
        echo "    ✅ 类型: CO 传感器 (co)"
        echo "    📊 DeviceID 建议: CO_SENSOR_$(date +%Y%m%d_%H%M%S)"

        value=$(echo "$line" | sed -n 's/.*CO:\([0-9]*\)ppm.*/\1/p')
        echo "    ☠️  CO: ${value} ppm"

    elif echo "$line" | grep -qE "SMOKE:[0-9]+ppm"; then
        echo "    ✅ 类型: 烟雾传感器 (smoke)"
        echo "    📊 DeviceID 建议: SMOKE_SENSOR_$(date +%Y%m%d_%H%M%S)"

    elif echo "$line" | grep -qE "LEVEL:[0-9]+mm"; then
        echo "    ✅ 类型: 液位传感器 (liquid_level)"
        echo "    📊 DeviceID 建议: LEVEL_SENSOR_$(date +%Y%m%d_%H%M%S)"

    elif echo "$line" | grep -qE "COND:[0-9]+\.[0-9]+mS"; then
        echo "    ✅ 类型: 电导率传感器 (conductivity)"
        echo "    📊 DeviceID 建议: COND_SENSOR_$(date +%Y%m%d_%H%M%S)"

    elif echo "$line" | grep -qE "FLOW:[0-9]+\.[0-9]+L"; then
        echo "    ✅ 类型: 流速传感器 (flow)"
        echo "    📊 DeviceID 建议: FLOW_SENSOR_$(date +%Y%m%d_%H%M%S)"

    else
        echo "    ⚠️  类型: 未知格式"
        echo "    💡 请检查数据格式是否正确"
    fi

    echo ""

    # 每读取 5 条数据后询问是否继续
    if [ $((count % 5)) -eq 0 ]; then
        echo "已读取 $count 条数据。继续? (y/n) [默认: y]"
        read -t 5 -n 1 response
        if [ "$response" = "n" ] || [ "$response" = "N" ]; then
            break
        fi
        echo ""
    fi
done

exec 3<&-

echo ""
echo "============================================================"
echo "✅ 识别完成，共读取 $count 条数据"
echo "============================================================"
echo ""
echo "📝 支持的传感器类型:"
echo "   1. temperature   - 温湿度传感器  (格式: R:XXX.XRH YYY.YC)"
echo "   2. co2           - CO2传感器     (格式: CO2:XXXppm)"
echo "   3. co            - CO传感器      (格式: CO:XXXppm)"
echo "   4. smoke         - 烟雾传感器    (格式: SMOKE:XXXppm)"
echo "   5. liquid_level  - 液位传感器    (格式: LEVEL:XXXmm)"
echo "   6. conductivity  - 电导率传感器  (格式: COND:XX.XmS)"
echo "   7. flow          - 流速传感器    (格式: FLOW:XX.XL)"
echo ""
